package com.qf.business.redevnlopes.core.task;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义的分布式定时任务处理器
 * author Ken
 * create_time 2022/9/29
 */
//@Component
public class MyXxlJob {

    private static List<Integer> datas = new ArrayList<>();

    static {
        for (int i = 1; i <= 10000; i++) {
            datas.add(i);
        }
    }

    @XxlJob("myhandler")
    public void handler(){
        //处理1千万条数据
        System.out.println("任务已经执行！！！");
        int index = XxlJobHelper.getShardIndex();
        int total = XxlJobHelper.getShardTotal();
        System.out.println("当前机器的下标：" + index + " 总共多少台：" + total);

        int size = datas.size();
        //每台机器要处理的数量
        int handlerNumber = size / total;

        List<Integer> handlerList = null;
        if(total - 1 == index) {
            handlerList = datas.subList(index * handlerNumber, size);
        } else {
            handlerList = datas.subList(index * handlerNumber, (index + 1) * handlerNumber);
        }

        System.out.println("本台机器要处理的数据范围：" + handlerList.get(0) + " ~ " + handlerList.get(handlerList.size() - 1));
    }
}
